

import 'package:dio/dio.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../SPutil.dart';
import '../Utils.dart';
import '../Settings.dart';
import 'package:get/get.dart' hide Response;


class DioInterceptor extends InterceptorsWrapper {

  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    super.onRequest(options, handler);

    var token = SPutil.getLocalStorage(Settings.localStorageKeys["token"]);
    if(!Utils.isEmpty(token)){
      options.headers[Settings.headerTokenKey] = token;
    }
    if(options.extra["systemNo"]!=null){
      options.headers['systemNo'] = options.extra["systemNo"];
    }else{
      options.headers['systemNo'] = Settings.systemNo;
    }
  }
  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    super.onResponse(response, handler);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    super.onError(err, handler);

    switch(err.type){
      case DioErrorType.receiveTimeout:
        {
          //响应超时
          EasyLoading.showToast('响应超时，请稍后再试');
        }
        break;
      case DioErrorType.connectTimeout:
        {
          //链接超时
          EasyLoading.showToast('链接超时，请稍后再试');
        }
        break;
      case DioErrorType.sendTimeout:
        {
          //发送超时
          EasyLoading.showToast('发送超时，请稍后再试');
        }
        break;
      case DioErrorType.cancel:
        {
          //请求取消
          // EasyLoading.showToast('');
        }
        break;
      case DioErrorType.response:
        {
          if(err.response?.statusCode==401){

            EasyLoading.showToast('登录已过期，请重新登录');
            Get.deleteAll();
            Get.offAllNamed("/userLogin");
            // NavigatorState? navigatorState = MyApp.navigatorKey.currentState;
            // if (navigatorState != null) {
            //   navigatorState.pushAndRemoveUntil(MaterialPageRoute(builder: (context) =>const UserLoginPage()),(route) => false);
            // }

          }else if(err.response?.statusCode==400){
            EasyLoading.showToast('请求错误');
          }else if(err.response?.statusCode==403){
            EasyLoading.showToast('拒绝访问');
          }else if(err.response?.statusCode==404){
            EasyLoading.showToast('很抱歉，资源未找到');
          }else if(err.response?.statusCode==405){
            EasyLoading.showToast('请求方法未允许');
          }else if(err.response?.statusCode==408){
            EasyLoading.showToast('请求超时');
          }else if(err.response?.statusCode==500){
            EasyLoading.showToast('服务器端出错');
          }else if(err.response?.statusCode==501){
            EasyLoading.showToast('网络未实现');
          }else if(err.response?.statusCode==502){
            EasyLoading.showToast('网络错误');
          }else if(err.response?.statusCode==503){
            EasyLoading.showToast('服务不可用');
          }else if(err.response?.statusCode==504){
            EasyLoading.showToast('网络超时');
          }else if(err.response?.statusCode==505){
            EasyLoading.showToast('网络超时');
          }else{
            EasyLoading.showToast('请求失败');
          }
        }
        break;
      case DioErrorType.other:
        {
          EasyLoading.showToast('响应超时，请稍后再试');
        }
        break;

    }

  }
}
